<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\SysItem;
use backend\models\AfterService;
use backend\models\AdminInfo;
use backend\models\OperatorRelation;
use yii\data\Sort;

/**
 * SysItemSearch represents the model behind the search form about `backend\models\SysItem`.
 */
class AfterServiceSearch extends AfterService
{
    /**
     * @inheritdoc
     */
		 public function rules()
		    {    	
		        return [
		            [['id', 'status'], 'integer'],
		            [['trade_class'],'integer'],
		            [['username','phone','customer_service'], 'string', 'max' => 20],
		            [['trade_no','contact_time'], 'string', 'max' => 192],
		            ['status','default','value'=>0],
		            [['updatetime','remark','customer_service','createtime','updatetime','content',],'safe'],
		        ];
		    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $this->load($params);
        if($this->trade_class==0){
            $this->trade_class=1;//默认为普通订单
    	}
    	$condition="pay_trade.trade_class= {$this->trade_class} ";
    	$user_id=Yii::$app->user->identity->user_id;
    	$user_type = Yii::$app->user->identity->user_type;
    	//如果是非管理员，则根据地址过滤
    	if ($user_type != 1 ){//非后台管理员
	    	if(\Yii::$app->user->can('after-service')){
	    		//运营商地址筛选
	    	    if(intval($user_id)>0){
	    	        $ifxgt=OperatorRelation::getifXat();//香港 澳门 台湾   直接in('台湾省','香港特别行政区','澳门特别行政区')
	    	        $ifhascity=OperatorRelation::ifhascity();
	    	        $ifhaszxsCountry=OperatorRelation::ifhaszxsCountry();
	    	        $ifhascountry=OperatorRelation::ifhascountry();
	    	        $ifhasprovince=OperatorRelation::ifhasprovince();
	    	        if($ifhasprovince){
	    	            $provinceStr=OperatorRelation::getProvinceNameById();
	    	            $condition.=" and SUBSTR(SUBSTRING_INDEX(address,' ',2),CHAR_LENGTH(SUBSTRING_INDEX(address,' ',1))+1) in ({$provinceStr})";
	    	        }
	    	        if($ifhascity){
	    	            $cityStr=OperatorRelation::getCityNameByIdNprovincename($user_id);
	    	            if($ifhasprovince){
	    	                $condition.=" or SUBSTR(SUBSTRING_INDEX(address,' ',2),CHAR_LENGTH(SUBSTRING_INDEX(address,' ',1))+1) in ({$cityStr})";
	    	            }else{
	    	                $condition.=" and SUBSTR(SUBSTRING_INDEX(address,' ',2),CHAR_LENGTH(SUBSTRING_INDEX(address,' ',1))+1) in ({$cityStr})";
	    	            }
	    	        }
	    	        if($ifhaszxsCountry||$ifhascountry){
	    	            if($ifhasprovince||$ifhascity){
	    	                $a="or";
	    	            }else{
	    	                $a="and";
	    	            }
	    	            $countryStr=OperatorRelation::getcountryNameById();
	    	            if($ifhaszxsCountry){//有直辖市 也有区县 合并条件
	    	                if($ifhascountry){
	    	                    //$condition.=$a."  (SUBSTR(SUBSTRING_INDEX(address,' ',2),CHAR_LENGTH(SUBSTRING_INDEX(address,' ',1))+1) in ({$countryStr}) or SUBSTR(SUBSTRING_INDEX(address,' ',3),CHAR_LENGTH( SUBSTRING_INDEX(address,' ',2))+1) in ({$countryStr}))";
	    	                    $condition.=$a."  (SUBSTR(address,1,CHAR_LENGTH(SUBSTRING_INDEX(address,' ',2))) in ({$countryStr}) or SUBSTR(address,1,CHAR_LENGTH(SUBSTRING_INDEX(address,' ',3))) in ({$countryStr}))";
	    	                }else{
	    	                    //$condition.=$a."  SUBSTR(SUBSTRING_INDEX(address,' ',2),CHAR_LENGTH(SUBSTRING_INDEX(address,' ',1))+1) in ({$countryStr}) ";
	    	                    $condition.=$a."  SUBSTR(address,1,CHAR_LENGTH(SUBSTRING_INDEX(address,' ',2))) in ({$countryStr}) ";
	    	                }
	    	            }else{
	    	                if($ifhascountry){
	    	                    //  $condition.=$a."  SUBSTR(SUBSTRING_INDEX(address,' ',3),CHAR_LENGTH( SUBSTRING_INDEX(address,' ',2))+1) in ({$countryStr})";
	    	                    $condition.=$a."  SUBSTR(address,1,CHAR_LENGTH(SUBSTRING_INDEX(address,' ',3))) in ({$countryStr})";
	    	                }
	    	            }
	    	        }
	    	    }
	    	} 
    	}  
    	/* $sql=$query->createCommand()->getRawSql();
    	return $sql; */
    	
    	$query = AfterService::find()->joinWith('payTrade')->where($condition)->orderBy("afterservice_order.id desc");
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
	        'pagination'=>['pageSize' => '10'],
        	'sort'=>false
        ]);
 
        
       

        if (!$this->validate()) {
            return $dataProvider;
        }
	
        $query->andFilterWhere([
            'afterservice_order.id' => $this->id,
        ]);
        $query->andFilterWhere(['like', 'afterservice_order.username', $this->username])
            ->andFilterWhere(['like', 'afterservice_order.phone', $this->phone])
            ->andFilterWhere(['like', 'afterservice_order.trade_no', $this->trade_no])
            ->andFilterWhere(['like', 'FROM_UNIXTIME(afterservice_order.createtime, "%Y-%m-%d")', $this->createtime])
           ;
        $query->andWhere([ 'pay_trade.trade_class'=> $this->trade_class]);
        if($this->status==5){
        }else{
            if($this->status){
                $query->andFilterWhere(['like', 'afterservice_order.status', $this->status]);
            }
        	
        }
        if($this->status==""){
        	$this->status=5;
        }
        return $dataProvider;
    }
    
}
